using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.Models;
using Web.Infrastructure;
using Web.Infrastructure.Storage;

namespace Web.Controllers
{   
    public class CategoriesController : Controller
    {
		private ISession _db;
        private IReadOnlySession _readOnlyDb;
        
		public CategoriesController(ISession session, IReadOnlySession readOnlySession)
        {
            _db = session;
            _readOnlyDb = readOnlySession;
        }

		//
        // GET: /Categories/

        public ViewResult Index()
        {
            return View(_readOnlyDb.All<Category>().Include(category => category.Products).OrderBy(x => x.Name).ToList());
        }

        //
        // GET: /Categories/Details/5

        public ViewResult Details(int id)
        {
            Category item = _readOnlyDb.All<Category>().Include(category => category.Products).Single(x => x.Id == id);
            return View(item);
        }

        //
        // GET: /Categories/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Categories/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            var item = new Category();
            var whiteList = new string[] { "Name" }; // todo - add editable properties to this list
            if (TryUpdateModel(item, whiteList, collection.ToValueProvider()))
			{
				if (ModelState.IsValid)
				{
						_db.Add<Category>(item);
					_db.CommitChanges();

					TempData.Add(Constants.TempDataMessage, "Added successfully");
					return RedirectToAction("Index");  
				}
			}
            return View(item);
        }
        
        //
        // GET: /Categories/Edit/5
 
        public ActionResult Edit(int id)
        {
            Category category = _readOnlyDb.Single<Category>(x => x.Id == id);
            return View(category);
        }

        //
        // POST: /Categories/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var item = _db.Single<Category>(x => x.Id == id);
            var whiteList = new string[] { "Name" }; // todo - add editable properties to this list
			if (TryUpdateModel(item, whiteList, collection.ToValueProvider()))
			{
				if (ModelState.IsValid)
				{
					_db.CommitChanges();
					TempData.Add(Constants.TempDataMessage, "Saved successfully");
					return RedirectToAction("Index");
				}
			}
            return View(item);
        }

        //
        // GET: /Categories/Delete/5
 
        public ActionResult Delete(int id)
        {
            Category category = _readOnlyDb.Single<Category>(x => x.Id == id);
            return View(category);
        }

        //
        // POST: /Categories/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            _db.Delete<Category>(x => x.Id == id);
            _db.CommitChanges();
            return RedirectToAction("Index");
        }
    }
}